/**
 *  Version:     @(#)aio_log.c    0.2.11 20/03/2007
 *
 *  Authors:     Hailong Xia <hlxxxx@gmail.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty
 *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *  the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *  Boston, MA 02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include "aio_config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>

#include "aio_log.h"

#ifdef HAVE_SYSLOG_H

#define SYSLOG_NAMES
#include <syslog.h>

static int log_to_syslog = 0;

/* Log anything (except mail) of level info or higher.  messages */
static int syslog_level = LOG_USER | LOG_INFO;

void
log_set_tosyslog(int tosyslog)
{
        log_to_syslog = tosyslog;
}

void
log_set_syslog_level(const char *level)
{
        int i, found = 0;

        for (i = 0; facilitynames[i].c_name != NULL; i++) {
                if (!strcmp(level, facilitynames[i].c_name)) {
                        syslog_level = facilitynames[i].c_val | LOG_DEBUG;
                        found = 1;
                }
        }

        if (found == 0) {
                log_warn("Failed to set syslog level %s\n", level);
        }
}

int
log_get_syslog_level()
{
        return syslog_level;
}

#endif /* ifdef HAVE_SYSLOG_H */

static FILE *log_output_file = 0;

void
log_set_output_file(const char *filedir)
{
        log_output_file = fopen(filedir, "w+");
        if (log_output_file == 0) {
                log_warn("Failed to open log output file %s\n", filedir);
        }
}

int log_silence_level = 0;

void
log_set_silence_level(int level)
{
        log_silence_level = level;
}

void
log_f(const char *subsys, const char *file, int line, const char *format, ...)
{
        va_list ap;
        time_t t;
        struct tm *tim;

        va_start(ap, format);

#ifdef HAVE_SYSLOG_H 
        if (!log_to_syslog) {
#endif
                /* not running as daemon - log to STDERR */
                if (log_output_file == 0)
                        log_output_file = stderr;
                time(&t);
                tim = localtime(&t);
                fprintf(log_output_file, "%2.2i:%2.2i:%2.2i %s:%s:%i ", tim->tm_hour,
                        tim->tm_min, tim->tm_sec, subsys, file, line);
                vfprintf(log_output_file, format, ap);
                /* fprintf(log_output_file, "\n"); */ 
                fflush(log_output_file);
#ifdef HAVE_SYSLOG_H 
        } else {
                /* running as daemon - log via SYSLOG facility */
                char string[AIO_LOG_BUFSIZE];
                vsnprintf(string, sizeof(string), format, ap);
                syslog(syslog_level, "%s:%s %i:%s", subsys, file, line, string);
        }
#endif
        va_end(ap);
}

/**
 * Log Usage: 
 * int main()
 * {
 *       Log to a specfiy file. Default output to stderr.
 *       log_set_output_file("./a.log");
 *
 *       Set silence level, n = 0/1/2/3/4
 *       log_set_silence_level(n);
 *
 *#ifndef _HAVE_SYSLOG_H
 *       If you want to log to syslog, call below function.
 *       log_set_tosyslog(1);
 *
 *       The default syslog_level is "LOG_USER | LOG_INFO".
 *       If you want to change syslog level, you can call below function.
 *       log_set_syslog_level("local5");
 *#endif
 *       debugp("debug log test...\n");
 *       log_info("info log test...\n");
 *       log_warn("warn log test...\n");
 *       log_err("error log test...\n");
 *       return 0;
 * }
 */

